home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / iphdr.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  4KB  |  183 lines

  1. /* IP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "ip.h"
  7. #include "internet.h"
  8.  
  9. /* Convert IP header in host format to network mbuf
  10.  * If cflag != 0, take checksum from structure,
  11.  * otherwise compute it automatically.
  12.  */
  13. struct mbuf *
  14. htonip(ip,data,cflag)
  15. register struct ip *ip;
  16. struct mbuf *data;
  17. int cflag;
  18. {
  19.     int16 hdr_len;
  20.     struct mbuf *bp;
  21.     register char *cp;
  22.     int16 fl_offs;
  23.  
  24.     hdr_len = IPLEN + ip->optlen;
  25.     if(hdr_len > IPLEN + IP_MAXOPT)
  26.         hdr_len = IPLEN + IP_MAXOPT;
  27.     if((bp = pushdown(data,hdr_len)) == NULLBUF){
  28.         free_p(data);
  29.         return NULLBUF;
  30.     }
  31.     cp = bp->data;
  32.     
  33.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  34.     *cp++ = ip->tos;
  35.     cp = put16(cp,ip->length);
  36.     cp = put16(cp,ip->id);
  37.     fl_offs = ip->offset >> 3;
  38.     if(ip->flags.congest)
  39.         fl_offs |= 0x8000;
  40.     if(ip->flags.df)
  41.         fl_offs |= 0x4000;
  42.     if(ip->flags.mf)
  43.         fl_offs |= 0x2000;
  44.  
  45.     cp = put16(cp,fl_offs);
  46.     *cp++ = ip->ttl;
  47.     *cp++ = ip->protocol;
  48.     if(cflag){
  49.         /* Use checksum from host structure */
  50.         cp = put16(cp,ip->checksum);
  51.     } else {
  52.         /* Clear checksum for later recalculation */
  53.         *cp++ = 0;
  54.         *cp++ = 0;
  55.     }
  56.     cp = put32(cp,ip->source);
  57.     cp = put32(cp,ip->dest);
  58.     if(ip->optlen != 0)
  59.         memcpy(cp,ip->options,min(ip->optlen,IP_MAXOPT));
  60.  
  61.     /* If requested, recompute checksum and insert into header */
  62.     if(!cflag)
  63.         put16(&bp->data[10],cksum(NULLHEADER,bp,hdr_len));
  64.  
  65.     return bp;
  66. }
  67. /* Extract an IP header from mbuf */
  68. int
  69. ntohip(ip,bpp)
  70. register struct ip *ip;
  71. struct mbuf **bpp;
  72. {
  73.     int ihl;
  74.     int16 fl_offs;
  75.     char ipbuf[IPLEN];
  76.  
  77.     if(pullup(bpp,ipbuf,IPLEN) != IPLEN)
  78.         return -1;
  79.  
  80.     ip->version = (ipbuf[0] >> 4) & 0xf;
  81.     ip->tos = ipbuf[1];
  82.     ip->length = get16(&ipbuf[2]);
  83.     ip->id = get16(&ipbuf[4]);
  84.     fl_offs = get16(&ipbuf[6]);
  85.     ip->offset = (fl_offs & 0x1fff) << 3;
  86.     ip->flags.mf = (fl_offs & 0x2000) ? 1 : 0;
  87.     ip->flags.df = (fl_offs & 0x4000) ? 1 : 0;
  88.     ip->flags.congest = (fl_offs & 0x8000) ? 1 : 0;
  89.     ip->ttl = ipbuf[8];
  90.     ip->protocol = ipbuf[9];
  91.     ip->checksum = get16(&ipbuf[10]);
  92.     ip->source = get32(&ipbuf[12]);
  93.     ip->dest = get32(&ipbuf[16]);
  94.  
  95.     ihl = (ipbuf[0] & 0xf) << 2;
  96.     if(ihl < IPLEN){
  97.         /* Bogus packet; header is too short */
  98.         ip->optlen = 0;
  99.         return -1;
  100.     }
  101.     if ( (ip->optlen = ihl - IPLEN) != 0 ) {
  102.         if ( pullup(bpp,ip->options,ip->optlen) < ip->optlen )
  103.             return -1;
  104.     }
  105.     return ihl;
  106. }
  107. /* Perform end-around-carry adjustment */
  108. int16
  109. eac(sum)
  110. register int32 sum;    /* Carries in high order 16 bits */
  111. {
  112.     register int16 csum;
  113.  
  114.     while((csum = sum >> 16) != 0)
  115.         sum = csum + (sum & 0xffffL);
  116.     return (int16) (sum & 0xffffl);    /* Chops to 16 bits */
  117. }
  118.  
  119. /* Checksum a mbuf chain, with optional pseudo-header */
  120.  
  121. int16 cksum(
  122.     struct pseudo_header *ph,
  123.     struct mbuf *m,
  124.     int16 len)
  125. {
  126.     register int16 cnt, total;
  127.     register int32 sum, csum;
  128.     register char *up;
  129.     int16 csum1;
  130.     int swap = 0;
  131.  
  132.     sum = 0l;
  133.  
  134.     /* Sum pseudo-header, if present */
  135.     if(ph != NULLHEADER){
  136.         sum = hiword(ph->source);
  137.         sum += loword(ph->source);
  138.         sum += hiword(ph->dest);
  139.         sum += loword(ph->dest);
  140.         sum += uchar(ph->protocol);
  141.         sum += ph->length;
  142.     }
  143.     /* Now do each mbuf on the chain */
  144.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  145.         cnt = min(m->cnt, len - total);
  146.         up = (char *)m->data;
  147.         csum = 0;
  148.  
  149.         if(((long)up) & 1){
  150.             /* Handle odd leading byte */
  151.             if(swap)
  152.                 csum = uchar(*up++);
  153.             else
  154.                 csum = (int16)(uchar(*up++) << 8);
  155.             cnt--;
  156.             swap = !swap;
  157.         }
  158.         if(cnt > 1){
  159.             /* Have the primitive checksumming routine do most of
  160.              * the work. At this point, up is guaranteed to be on
  161.              * a short boundary
  162.              */
  163.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  164.             if(swap)
  165.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  166.             csum += csum1;
  167.         }
  168.         /* Handle odd trailing byte */
  169.         if(cnt & 1){
  170.             if(swap)
  171.                 csum += uchar(up[--cnt]);
  172.             else
  173.                 csum += (int16)(uchar(up[--cnt]) << 8);
  174.             swap = !swap;
  175.         }
  176.         sum += csum;
  177.         total += m->cnt;
  178.     }
  179.     /* Do final end-around carry, complement and return */
  180.     return (int16)(~eac(sum) & 0xffff);
  181. }
  182.  
  183.